﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using JinJun.YShowReception.BLL.IService;
using JinJun.YShowReception.Model;
using JinJun.YShowReception.Web.Models;

namespace JinJun.YShowReception.Web.Controllers
{
    public class LoginController : Controller
    {
        private IUserInfoService _userInfoService;
        public LoginController(IUserInfoService userInfoService)
        {
            _userInfoService = userInfoService;
        }


        /// <summary>
        /// 用户登录方法-不保存密码/读取所有信息
        /// </summary>
        /// <param name="adminInfo"></param>
        /// <returns></returns>
        public JsonResult LoginNotSavePwd(UserInfo userInfo)
        {
            UserInfo user = _userInfoService.LoginUserInfo(userInfo);
            //创建操作类对象
            Operate operate = new Operate();
            //调用用户登录方法
            operate.Success = user != null;
            //登录成功
            if (operate.Success)
            {
                UserInfoContext.userInfoContext.UserInfo= user;
            }
            return Json(operate);
        }



        /// <summary>
        /// 用户登录方法-保存密码/读取所有信息
        /// </summary>
        /// <param name="adminInfo"></param>
        /// <returns></returns>
        public JsonResult LoginSavePwd(UserInfo userInfo)
        {
            UserInfo user = _userInfoService.LoginUserInfo(userInfo);
            //创建操作类对象
            Operate operate = new Operate();
            //调用用户登录方法
            operate.Success = user!=null;
            //登录成功
            if (operate.Success)
            {
                //判断是否记住密码
                string userData = user.Id + "#" + user.UserName + "#" + user.Pwd;
                //数据放入ticket
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(60), true, userData);
                //数据加密
                string enyTicket = FormsAuthentication.Encrypt(ticket);
                //创建cookie对象
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
                if (ticket.IsPersistent)
                {
                    cookie.Expires = ticket.Expiration;
                }
                //将cookie存入响应上下文中
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                UserInfoContext.userInfoContext.UserInfo = user;                     
            }
            return Json(operate);
        }


        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult AddUserInfo(UserInfo userInfo)
        {
            //创建操作类对象
            Operate operate = new Operate();
            //调用用户注册
            operate.Success = (_userInfoService.AddUserInfo(userInfo)) > 0;
            return Json(operate);
        }


        /// <summary>
        /// 查询手机号
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult CheckUserTel(UserInfo userInfo)
        {
            //创建操作类对象
            Operate operate = new Operate();
            //调用查询手机号
            operate.Success = (_userInfoService.CheckUserTel(userInfo)) > 0;
            return Json(operate);
        }


        /// <summary>
        /// 查询用户名
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult CheckUserName(UserInfo userInfo)
        {
            //创建操作类对象
            Operate operate = new Operate();
            //调用查询用户名
            operate.Success = (_userInfoService.CheckUserName(userInfo)) > 0;
            return Json(operate);
        }
    }
}